﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionReport
    {
        GestionRestaurant restaurant = new GestionRestaurant();
        GestionPreparationPlat plat = new GestionPreparationPlat();

        #region Dashboard

        public Dictionary<string, double> ChiffreAffaireParRestaurant()
        {
            try
            {
                using (Context db = new Context())
                {
                    var tab = new Dictionary<string, double>();
                    List<Restaurant> restaurants = (from r in db.Restaurants select r).ToList();

                    foreach (Restaurant resto in restaurants )
                    {
                        double chiffreAffaire = 0;
                        List<Commande> commandes = (from c in db.Commandes.Include("Serveur.Restaurant")
                                     where c.Serveur.Restaurant.Id == resto.Id && c.Prix > 0
                                     select c).ToList();

                        foreach (Commande com in commandes)
                        {
                            chiffreAffaire += com.Prix;
                        }
                        tab.Add(resto.Nom, chiffreAffaire);
                    }
                    return (tab);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur DB : Echec Recuperation des informations du rapport", ex);
            }
        }

        public Dictionary<string, int> NombreCommande(string scope)
        {
            try
            {
                DateTime approuved;
                switch (scope)
                {
                    case "jour":
                        approuved = DateTime.Today;
                        break;
                    case "semaine":
                        approuved = DateTime.Today.AddDays(-7);
                        break;
                    case "mois":
                        approuved = DateTime.Today.AddDays(-30);
                        break;
                    default:
                        approuved = DateTime.Today.AddDays(-365);
                        break;
                }
                approuved.AddMilliseconds(00);
                using (Context db = new Context())
                {
                    var tab = new Dictionary<string, int>();

                    List<Restaurant> restaurants = (from r in db.Restaurants select r).ToList();

                    foreach (Restaurant resto in restaurants)
                    {
                        int nombre = (from co in db.Commandes.Include("Serveur.Restaurant")
                                      where co.Date.Value >= approuved
                                      & co.Serveur.Restaurant.Id == resto.Id
                                      & co.Prix > 0
                                      select co).Count();

                        tab.Add(resto.Nom, nombre);
                    }
                    return (tab);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur DB : Echec Recuperation des informations du rapport", ex);
            }
        }

        public Dictionary<string, int> PlatLePlusCommandes(string scope)
        {
            try
            {
                DateTime approuved;
                switch (scope)
                {
                    case "jour":
                        approuved = DateTime.Today;
                        break;
                    case "semaine":
                        approuved = DateTime.Today.AddDays(-7);
                        break;
                    case "mois":
                        approuved = DateTime.Today.AddDays(-30);
                        break;
                    default:
                        approuved = DateTime.Today.AddDays(-365);
                        break;
                }

                using (Context db = new Context())
                {
                    var tab = new Dictionary<string, int>();

                    List<PreparationPlat> prepPlat = (from p in db.PreparationPlats  select p).ToList();

                    foreach (PreparationPlat prep in prepPlat)
                    {

                        int nombre = (from pp in db.PreparationPlats
                                      where pp.DateSoumission >= approuved
                                      select pp).Count();

                        // int nombre = query.Count();
                        tab.Add(prep.Plat.Nom, nombre);
                    }
                    return (tab);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur DB : Echec Recuperation des informations du rapport", ex);
            }
        }

        #endregion

        #region Serveur
        public Dictionary<string, int> ServeurParRestaurants()
        {
            try
            {
                using (Context db = new Context())
                {
                    var tab = new Dictionary<string, int>();

                    foreach (Poco.Restaurant resto in restaurant.Lister())
                    {
                        int nombreServeurs = (from se in db.Utilisateurs.OfType<Serveur>()
                                              where se.Restaurant == resto
                                              select se).Count();

                        tab.Add(resto.Nom, nombreServeurs);
                    }
                    return (tab);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur DB : Echec Recuperation des informations du rapport", ex);
            }
        }
        #endregion

        #region Cuisinier
        public Dictionary<string, int> CuisiniersParRestaurants()
        {
            try
            {
                using (Context db = new Context())
                {
                    var tab = new Dictionary<string, int>();

                    foreach (Poco.Restaurant resto in restaurant.Lister())
                    {
                        int nombreCuisiniers = (from cu in db.Utilisateurs.OfType<Cuisinier>()
                                                where cu.Restaurant == resto
                                                select cu).Count();

                        tab.Add(resto.Nom, nombreCuisiniers);
                    }
                    return (tab);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur DB : Echec Recuperation des informations du rapport", ex);
            }
        }
        #endregion
    }
}
